package gen

import (
	"github.com/lab-online/pkg/logger"
	"github.com/lab-online/pkg/resp"
)

type ErrorMap map[string][]error

func ParseCodeMapFromErrors(errorMap ErrorMap) CodeMap {
	codeMap := CodeMap{}

	for mod, errors := range errorMap {
		codeMap[mod] = map[int]string{}
		for _, error := range errors {
			code, msg := parseError(error)
			codeMap[mod][code] = msg
		}
	}

	return codeMap
}

func parseError(e error) (code int, msg string) {
	switch e := e.(type) {
	case resp.RESTError:
		return e.GetCode(), e.GetMessage()
	case resp.DatabaseError:
		return e.GetCode(), e.GetMessage()
	default:
		logger.Error("unknown error", e)
		panic("unknown error")
	}
}
